//
//  ViewController.m
//  第三题
//
//  Created by dllo on 16/6/3.
//  Copyright © 2016年 dllo. All rights reserved.-
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

-(BOOL)prefersStatusBarHidden
{
    return YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self addView];
    
}

-(void)addView
{
    CGFloat weidth = self.view.frame.size.width / 5;
    CGFloat height = self.view.frame.size.height / 8;
    for (int i = 1; i <= 8; i++) {
        for (int j = 1; j <= 5; j++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(weidth * (j - 1), height * (i - 1), weidth, height);
            button.backgroundColor = [self colorOfRandom];
            
            //设置边框为圆角
            [button.layer setMasksToBounds:YES];
            [button.layer setCornerRadius:10.0];
            
            //设置边框宽度
            [button.layer setBorderWidth:0.5];
            
            //设置边框颜色
            CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
            CGColorRef colorref = CGColorCreate(colorSpace, (CGFloat[]){0, 0, 0, 1});
            [button.layer setBorderColor:colorref];
            
            [self.view addSubview:button];
            [button addTarget:self action:@selector(colorOfRandom:) forControlEvents:UIControlEventTouchUpInside];
            [button release];
        }
    }
}

-(UIColor *)colorOfRandom
{
    int R = arc4random() % 256;
    int G = arc4random() % 256;
    int B = arc4random() % 256;
    return [UIColor colorWithRed:R/255.0  green:G/255.0  blue:B/255.0  alpha:1];
}

-(void)colorOfRandom:(UIButton *)button
{
    NSArray *array = button.superview.subviews;
    NSEnumerator *enrmerator = [array objectEnumerator];
    UIButton *object = [UIButton buttonWithType:UIButtonTypeCustom];

    while (object = [enrmerator nextObject]) {
        object.backgroundColor = [self colorOfRandom];
        
    }
}



@end







